﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Zaplon125p
{
    public partial class Form1 : Form
    {
        String[] Ports;
        Byte uc_RespTask = 0;
        int n_PortErr = 0;
        const Byte Preamb1 = 0x2D;
        const Byte Preamb2 = 0xD2;
        const Byte DID_ZAPLON = 0x01;
        Byte[] RecFrameBuf = new byte[10];
        Byte uc_ByteCnt = 0;
        Byte ucCrc = 0;
        Byte Reg = 1;

        int Speed;
        int Press;
        int ProgSpeed;
        int ProgPress;
        int SetTime;
        int Mul;
        int CutOffObroty;
        int CutOffSet;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            fv_CheckPorts();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fv_GetRegister(1);

        }


        private void fv_GetRegister(byte uc_RegNr)
        {
            byte[] uc_Buf = new byte[20];
            byte[] uc_BufResp = new byte[20];
            uint un_Crc = 0x100;
            uc_Buf[0] = 0x2D;
            uc_Buf[1] = 0xD2;
            uc_Buf[2] = 0x01;
            uc_Buf[3] = 0xFF;
            uc_Buf[4] = uc_RegNr;
            un_Crc -= Convert.ToUInt16(uc_Buf[2] + uc_Buf[3] + uc_Buf[4]);
            un_Crc &= 0xFF;
            uc_Buf[5] = Convert.ToByte(un_Crc);
            serialPort1.Write(uc_Buf, 0, 6);
            //textBox1.Text += "\r\n";
              //Read(uc_BufResp, 0, 20);
            
        }


        private void fv_SetRegister(byte uc_RegNr,byte[] Buf)
        {
            byte[] uc_Buf = new byte[20];
            byte[] uc_BufResp = new byte[20];
            uint un_Crc = 0x100;
            uc_Buf[0] = 0x2D;
            uc_Buf[1] = 0xD2;
            uc_Buf[2] = 0x01;
            uc_Buf[3] = uc_RegNr;
            uc_Buf[4] = Buf[0];
            uc_Buf[5] = Buf[1];
            uc_Buf[6] = Buf[2];
            uc_Buf[7] = Buf[3];
            un_Crc -= Convert.ToUInt16(uc_Buf[2] + uc_Buf[3] + uc_Buf[4] + uc_Buf[5] + uc_Buf[6] + uc_Buf[7]);
            un_Crc &= 0xFF;
            uc_Buf[8] = Convert.ToByte(un_Crc);
            serialPort1.Write(uc_Buf, 0, 9);
            //textBox1.Text += "\r\n";
            //Read(uc_BufResp, 0, 20);

        }
        /**
         * 
         */
        private void fv_GetFrame(Byte Sign)
        {
            switch (uc_RespTask)
            {
                case 0:
                    {
                        if (Sign == Preamb1)
                        {
                            uc_RespTask = 1;
                        }
                    }break;
                case 1:
                    {
                        if (Sign == Preamb2)
                        {
                            uc_RespTask = 2;
                            uc_ByteCnt = 0;
                            ucCrc = 0;
                        }
                        else
                        {
                            uc_RespTask = 0;
                        }
                    }break;
                case 2:
                    {
                        if (uc_ByteCnt < 6)
                        {
                            RecFrameBuf[uc_ByteCnt] = Sign;
                            ucCrc += Sign; 
                            uc_ByteCnt++;
                        }
                        else
                        {
                            ucCrc += Sign;
                            if (ucCrc == 0)
                            {
                                fv_FrameInterpreter();
                            }
                            else
                            {

                            }
                            uc_RespTask = 0;
                        }
                    } break;
            }

        }

        private void fv_FrameInterpreter()
        {
            switch (RecFrameBuf[0])
            {
                case DID_ZAPLON:
                    {
                        switch (RecFrameBuf[1])
                        {
                            case 1:
                                {
                                    Press = (RecFrameBuf[2] << 8) + RecFrameBuf[3];
                                    label_Press.Text = Press.ToString();
                                    Speed = (RecFrameBuf[4] << 8) + RecFrameBuf[5];
                                    label_Speed.Text = Speed.ToString();
                                }break;
                            case 2:
                                {
                                    ProgPress = (RecFrameBuf[2] << 8) + RecFrameBuf[3];
                                    label_ProgPress.Text = ProgPress.ToString();
                                    ProgSpeed = (RecFrameBuf[4] << 8) + RecFrameBuf[5];
                                    label_ProgSpeed.Text = ProgSpeed.ToString();
                                } break;//ok
                            case 3:
                                {
                                    CutOffObroty = (RecFrameBuf[2] << 8) + RecFrameBuf[3];
                                    label_CutOffObroty.Text = CutOffObroty.ToString();
                                    if ((RecFrameBuf[4] & 0x01) != 0)
                                    {
                                        label_CutOffSet.Text = "ON";
                                    }
                                    else
                                    {
                                        label_CutOffSet.Text = "OFF";
                                    }
                                } break;
                            case 4:
                                {
                                    SetTime = (RecFrameBuf[2] << 8) + RecFrameBuf[3];
                                    label_SetTime.Text = SetTime.ToString();
                                    Mul = RecFrameBuf[4];
                                    label_Mul.Text = Mul.ToString();



                                    if ((RecFrameBuf[5] & 0x01) != 0)
                                    {
                                        label_Elektrodysza.Text = "ON";
                                    }
                                    else
                                    {
                                        label_Elektrodysza.Text = "OFF";
                                    }
                                    if ((RecFrameBuf[5] & 0x02) != 0)
                                    {
                                        label_Blokada.Text = "ON";
                                    }
                                    else
                                    {
                                        label_Blokada.Text = "OFF";
                                    }
                                } break;
                        } 
                    }break;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            int dat;
            byte[] uc_BufResp = new byte[1];
            if (serialPort1.IsOpen == true)
            {
                while (serialPort1.BytesToRead > 0)
                {
                    serialPort1.Read(uc_BufResp, 0, 1);
                    fv_GetFrame(uc_BufResp[0]);
                    //textBox1.Text += uc_BufResp[0].ToString() + " ; ";
                }
            }
            //dat = serialPort1.ReadByte();
        }

        private void timer_CheckPorts_Tick(object sender, EventArgs e)
        {
            //fv_CheckPorts();
        }


        private void fv_CheckPorts()
        {
            String[] LocalPorts;
            LocalPorts = System.IO.Ports.SerialPort.GetPortNames();
           // if (LocalPorts.)
            {
                Ports = LocalPorts;
                comboBox_PortName.Items.Clear();
                comboBox_PortName.Items.AddRange(Ports);
                if (comboBox_PortName.Items.Count > 0)
                {
                    //if (comboBox_PortName.Text.Length == 0)
                    {
                        comboBox_PortName.Text = comboBox_PortName.Items[0].ToString();
                    }
                }
                else
                {
                    if (n_PortErr >= 0)
                    {
                        n_PortErr = -1;
                        MessageBox.Show("Nie znaleziono portu szeregowego.", "UWAGA!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void button_OpenColsePort_Click(object sender, EventArgs e)
        {

            if (serialPort1.IsOpen == false)
            {
                button_OpenColsePort.Text = "Zamknij";
                serialPort1.PortName = comboBox_PortName.Text;
                serialPort1.Open();
            }
            else
            {
                button_OpenColsePort.Text = "Otwórz";
                serialPort1.Close();
            }
        }

        private void timer_SendFrame_Tick(object sender, EventArgs e)
        {

            if (serialPort1.IsOpen == true)
            {
                switch (Reg)
                {
                    case 1:
                        {
                            Reg = 2;
                        }break;
                    case 2:
                        {
                            Reg = 3;
                        } break;
                    case 3:
                        {
                            Reg = 4;
                        } break;
                    default:
                        {
                            Reg = 1;
                        }break;
                }
                fv_GetRegister(Reg);
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] Buf1 = {0,0,13,0};
            fv_SetRegister(104, Buf1);
        }
    }
}
